const vscode = require('vscode');
const tips = require('./tipsData')
//const util = require('./util');

/**
 * 自动提示实现，这里模拟一个很简单的操作
 * 当输入 this.dependencies.xxx时自动把package.json中的依赖带出来
 * 当然这个例子没啥实际意义，仅仅是为了演示如何实现功能
 * @param {*} document 
 * @param {*} position 
 * @param {*} token 
 * @param {*} context 
 */
function provideCompletionItems(document, position, token, context) {
    const line = document.lineAt(position);
    console.log("进入自动补全")
    // 只截取到光标位置为止，防止一些特殊情况
    const lineText = line.text.substring(0, position.character);
    
    //一下为所有的补全代码
    const R_HtmlStr = RegExp('htmlstr','gi')//注册一个正则表达式
    if (R_HtmlStr.test(lineText)) {
        const dependencies =tips.tipsData['htmlstr'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['htmlstr'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }


    const R_ObjTool = RegExp('objtool','gi')//注册一个正则表达式
    if (R_ObjTool.test(lineText)) {
       const dependencies =tips.tipsData['objtool'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['objtool'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_oSV = RegExp('osv','gi')//注册一个正则表达式
    if (R_oSV.test(lineText)) {
       const dependencies =tips.tipsData['osv'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['osv'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_Request = RegExp('request','gi')//注册一个正则表达式
    if (R_Request.test(lineText)) {
       const dependencies =tips.tipsData['request'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['request'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_Response = RegExp('response','gi')//注册一个正则表达式
    if (R_Response.test(lineText)) {
       const dependencies =tips.tipsData['response'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['response'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_Server = RegExp('server','gi')//注册一个正则表达式
    if (R_Server.test(lineText)) {
       const dependencies =tips.tipsData['server'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['server'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_Session = RegExp('session','gi')//注册一个正则表达式
    if (R_Session.test(lineText)) {
       const dependencies =tips.tipsData['session'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['session'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_UBB = RegExp('ubb','gi')//注册一个正则表达式
    if (R_UBB.test(lineText)) {
        const dependencies =tips.tipsData['ubb'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['ubb'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_Vfpweb = RegExp('vfpweb','gi')//注册一个正则表达式
    if (R_Vfpweb.test(lineText)) {
        const dependencies =tips.tipsData['vfpweb'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['vfpweb'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }

    const R_xlstool = RegExp('xlstool','gi')//注册一个正则表达式
    if (R_xlstool.test(lineText)) {
        const dependencies =tips.tipsData['xlstool'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['xlstool'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }
    
    const R_ddd = RegExp('ddd','gi')//注册一个正则表达式
    if (R_ddd.test(lineText)) {
       const dependencies =tips.tipsData['htmlstr'][0];
        ///var xxx = dependencies;
        console.log("返回补全列表")
        console.log(tips.tipsData['htmlstr'])
        var xxx = [];
        
       for(var p in dependencies){
           xxx.push ({label:p,insertText:p,detail:dependencies[p]}) 
       }
        console.log(xxx) 
       return xxx;
    }


}

/**
 * 光标选中当前自动补全item时触发动作，一般情况下无需处理
 * @param {*} item 
 * @param {*} token 
 */
function resolveCompletionItem(item, token) {
    console.log(item);
    return null;
}

module.exports = function (context) {
    // 注册代码建议提示，只有当按下“.”时才触发
    context.subscriptions.push(vscode.languages.registerCompletionItemProvider('foxpro', {
        provideCompletionItems,
        resolveCompletionItem
    }, '.'));
};

